<?php
/**
 * Copyright (c) 2010, Slawomir Mazur <mazursllawek@gmail.com>
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * o Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the Softisland nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Enter description here...
 *
 * @package     default
 * @copyright   Slawomir Mazur (c) 2010 (http://www.zend.com/zce.php?c=ZEND011643)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @author      Slawomir Mazur <mazursllawek@gmail.com>
 */

class Gp_View_Helper_Library extends Zend_View_Helper_Abstract
{
	const LATEST = 'latest';
	
	protected $_statuses = null;
	
	protected $_companies = null;
	
	public function library()
	{
		return $this;
	}
	
	/**
	 * Get library name by id
	 * 
	 * @param int $id
	 * @return string library name|empty
	 */
	public function getLibraryNameById($id)
	{
		$library = Gp_App::getModel('system/library')
					->find($id)
					->current();
					
		if (!empty($library)) {
			return $library->name;
		}
		return '';
	}
	
	/**
	 * Get library date by id
	 * 
	 * @param int $id
	 * @return string date
	 */
	public function getLibraryCreatedDateById($id)
	{
		$library = Gp_App::getModel('system/library')
					->find($id)
					->current();
					
		if (!empty($library)) {
			//return date('d-m-Y H:i', strtotime($library->created_at));
			return date('Y', strtotime($library->created_at));
		}
		return '';
	}
	
	/**
	 * Get library designed date by id
	 * 
	 * @param int $id
	 * @return string date
	 */
	public function getLibraryDesignedDateById($id)
	{
		$library = Gp_App::getModel('system/library')
					->find($id)
					->current();
					
		if (!empty($library)) {
			//return date('d-m-Y H:i', strtotime($library->created_at));
			return date('Y', strtotime($library->designed_at));
		}
		return '';
	}
	
	/**
	 * Get library provider by id
	 * 
	 * @param int $id
	 * @return string provider external link
	 */
	public function getLibraryProviderById($id) {
		$library = Gp_App::getModel('system/library')
					->find($id)
					->current();
					
		if (!empty($library)) {
			return $library->provider;
		}
		return '';
	}
	
	/**
	 * Get status label based on its ID
	 * 
	 * @param int $statusId
	 * @return string
	 */
	public function getLibraryStatusName($statusId)
	{
		$statuses = $this->getStatuses();
		if (isset($statuses[$statusId])) {
			return $statuses[$statusId];
		}
		return '';
	}
	
	public function getStatuses()
	{
		if (is_null($this->_statuses)) {
			$this->_statuses = Gp_App::getModel('system/library')->getStatuses();
		}
		
		return $this->_statuses; 
	}
	
	public function getAnnotationYears($libraryId = false)
	{
		return Gp_App::getModel('system/library')->getAnnotationYears($libraryId);		
	}
	
	public function getAnnotationYearsAsHTML($libraryId = false)
	{
		$years = $this->getAnnotationYears($libraryId);
		
		$html = '<option value="' . self::LATEST . '">Latest...</option>';
		foreach ($years as $year) {
			$html .= '<option value="' . $year . '">' . $year . '</option>';
		}
		return $html; 
	}
	
	/**
	 * 
	 * Load available companies
	 * 
	 * @return array
	 */
	public function getCompanies()
	{
		if (is_null($this->_companies)) {
			$companies = Gp_App::getModel('system/company')
				->getCollection();
				
			$this->_companies = array(0 => 'Choose company...');
			
			foreach ($companies as $company) {
				$this->_companies[$company['company_id']] = $company['name'];
			}
		}
		
		return $this->_companies;
	}
}